package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import Database.*;
import DataStorageClasses.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import com.mysql.*;

/**
 * Servlet implementation class addBusinessServlet
 */
@WebServlet("/addBusinessServlet")
public class addBusinessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		
		String busName = request.getParameter("busName");
    	String password = request.getParameter("password");
    	String email = request.getParameter("email");
    	String emailPWord = request.getParameter("emailPassword");
    	String telNum = request.getParameter("busNum");
    	String fax = request.getParameter("fax");
    	String cell = request.getParameter("cell");
    	String streetAd = request.getParameter("streetAd");
    	String city = request.getParameter("city");
    	String prov = request.getParameter("province");
    	String pCode = request.getParameter("postalCode");
    	
    	
    	Business b = dbConnect.addBusiness(busName, password, telNum, fax, 
    										cell, email, streetAd, city, prov, pCode, emailPWord);
  /*  	
    	dbConnect.addReviewer(busName, password, email);
    	if( dbConnect.authenticateReviewer(busName, password))
		{
			 session.setAttribute("subloggedIn", true);
			 session.setAttribute("subUser", busName);
			 session.setAttribute("subPass", password);
			 session.setAttribute("subEmail", email);
		}
		else
		{
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		*/
    	if( b != null)
    	{
    		//great!
    		//System.out.println("add business worked");
    		//session.setAttribute("login", true);
    		//session.setAttribute("busName", busName);
    		res.sendRedirect("/GoGoGadget/StartPage.jsp");
    	}
    	else
    	{
    		//System.out.println("add business didn't work");
    	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
